<?php
class Alerta_modelo extends CI_Model {
	
	var $nombre = '';
	var $apellidos = '';
	var $nivel = '';
	var $email ='';
	var $movil ='';
	var $idObra='';
	
	var $empresa='';

function __construct()
{
parent::__construct();


}

function insertar_alerta($idObra){
	
	
	
	$this->nombre = $this->input->post('nombre');
	$this->apellidos = $this->input->post('apellidos');
	$this->nivel = $this->input->post('nivel');
	$this->email=$this->input->post('email');
	$this->movil = $this->input->post('movil');
	$this->idObra = $idObra;
	
	if($this->input->post('empresa')!= NULL){
		$this->empresa=$this->input->post('empresa');
		
	}else{
		
		$this->empresa= '0';
		
		
	}
	
	
	
	
	
		
	$this->db->insert('alertas',$this);

	
	
		
}


function modificar_alerta($idAlerta){
	
	
	$this->load->helper(array('form', 'url','date'));
	
	
	
	$data ['nombre'] = $this->input->post('nombre');
	
	$data ['apellidos'] = $this->input->post('apellidos');
	$data ['nivel'] = $this->input->post('nivel');
	$data ['email'] = $this->input->post('email');
	$data ['movil'] = $this->input->post('movil');
	$data ['empresa'] = $this->input->post('empresa');
	

	
	
	$this->db->where('idAlerta', $idAlerta);
	$this->db->update('alertas',$data);
	
}



function listado_alertas($idObra){
	
	$query=$this->db->query('SELECT * FROM nivelalerta,alertas left outer join subcontrata ON idSubcontrata=empresa where nivel=idNivelA and idObra='.$idObra.'');
	return $query->result(); 
	
	
}

function listado_niveles(){
	
	$query=$this->db->get('nivelalerta');
	return $query->result(); 
	
	
}

function alertas(){
	
	$query=$this->db->query('Select * from alertas,nivelalerta where idNivelA=nivel  ');
	
	return $query->result(); 
	
	
	
}

function contador_tolerable(){
	
	$query=$this->db->query('Select count(idAlerta) as contador from alertas,nivelalerta where idNivelA=nivel and nivelAlerta=\'trivial\' ');
	
	return $query->result(); 
	
	
	
}

function contador_moderado(){
	
	$query=$this->db->query('Select count(idAlerta) as contador from alertas,nivelalerta where idNivelA=nivel and nivelAlerta IN (\'trivial\',\'tolerable\')');	
	return $query->result(); 
	
	
	
}

function contador_importante(){
	
	$query=$this->db->query('Select count(idAlerta) as contador from alertas,nivelalerta where idNivelA=nivel and nivelAlerta IN (\'trivial\',\'tolerable\',\'moderado\')');
	
	return $query->result(); 
	
	
	
}

function contador_intolerable(){
	
	$query=$this->db->query('Select count(idAlerta) as contador from alertas,nivelalerta where idNivelA=nivel and nivelAlerta IN (\'trivial\',\'tolerable\',\'moderado\',\'importante\')');
	
	return $query->result(); 
	
	
	
}



function emails_alertas(){
	
	$query=$this->db->query('Select idAlerta,email,nivelAlerta,nombre from alertas,nivelalerta where idNivelA=nivel ');
	
	return $query->result(); 
	
}


function emails_alertas_obra($idObra){
	
	$query=$this->db->query('Select idAlerta,email,nivelAlerta,nombre,apellidos from alertas,nivelalerta where idNivelA=nivel and idObra='.$idObra.'');
	
	return $query->result(); 
	
}


function insertarHistoricoA($idEmail,$idVisita,$fecha){
	
	$data['idAlerta']=$idEmail;
	$data['idVisita']=$idVisita;
	$data['fecha']=$fecha;
	
	$this->db->insert('historicoalertas',$data);
	
}


function listado_historicoA($idAlerta){
	
	$query=$this->db->query('Select * from alertas,historicoalertas,visita where historicoalertas.idAlerta='.$idAlerta.' and historicoalertas.idAlerta=alertas.idAlerta and historicoalertas.idVisita=visita.idVisita order by nombreVisita desc ');
	
	return $query->result(); 
	
	
}

function datos_alerta($idAlerta){
	
	$query=$this->db->query('Select * from alertas,nivelalerta where idAlerta='.$idAlerta.' and nivel=idNivelA ');
	
	return $query->result(); 
	
	
}

function datos_nivel($nivel){
	
	$query=$this->db->query('Select * from nivelalerta where idNivelA<='.$nivel.'');
	return $query->result(); 
}





}